﻿#ifndef PST_COMMONFUNCTION_H
#define PST_COMMONFUNCTION_H
//#include "PostProcessingModuleConfig.h"

#include <iostream>
#include <vector>
#include <QList>
#include <QString>
#include <QPixmap>
#include <vtkSmartPointer.h>
#include <opencv2/opencv.hpp>

class vtkWindow;
class vtkImageData;
class vtkActor;
class QTableWidget;
class QLayoutItem;
class QLayout;

namespace pst
{

    class CommonFunction
    {
    public:
        CommonFunction();

    public:
        // 字符串分割
        static void Stringsplit(const std::string& str, const std::string& splits,
            std::vector<std::string>& res);

        //去除字符串首尾空格
        static std::string trim(const std::string& s);

        //球坐标(角度制)转直角坐标-->点
        static void SphereToRectangularCoorSys(const double theta,
            const double phi, const double r, double& x, double& y, double& z);

        static void PolarToRectangularCoorSys(const double theta, const double r, double& x, double& y);

        static cv::Mat VTKImageDataToCVMat(vtkImageData* vtkImage);

        static void saveJPEGImageFromVTKScreen(vtkWindow* window, int width, int height, QString fileName, bool autoSize);

        static bool saveGifFromMatList(const QList<cv::Mat>& frames, const QString& outputFilename, int frameRate);

        //判断两个浮点数是否相等
        // ulp: units in the last place.
        template <typename T>
        typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
            IsAlmostEqual(T x, T y, int ulp = 2)
        {
            // the machine epsilon has to be scaled to the magnitude of the values used
            // and multiplied by the desired precision in ULPs (units in the last place)
            return std::fabs(x - y) <
                std::numeric_limits<T>::epsilon() * std::fabs(x + y) * ulp
                // unless the result is subnormal
                || std::fabs(x - y) < std::numeric_limits<T>::min();
        }

        static vtkSmartPointer<vtkActor> DeepCopyActorWithData(vtkActor* originalActor);

        // QPixmap转cv::Mat
        static cv::Mat CommonFunction::QPixmapToCVMat(const QPixmap& pixmap);

        /**
        * 导出 QTableWidget 数据到 CSV/TXT 文件
        * @param tableWidget 指向 QTableWidget 对象
        * @param filePath 文件保存路径（含扩展名，例如 "output.csv"）
        */
        static void exportTableWidgetToCSV(QTableWidget* tableWidget, const QString& filePath);

        // 提取最外层括号内的内容
        static bool extractContentBetweenParentheses(const QString& input, QString& output);

        //归一化一个角度到 [0,360)
        static double normalizeAngle(double angle)
        {
            // 对角度取 360 的模
            angle = std::fmod(angle, 360.0);
            // 如果结果为负，则加上 360，使其处于 [0, 360) 范围内
            if (angle < 0)
            {
                angle += 360.0;
            }
            return angle;
        }

        // 递归删除布局中的所有项
        static void clearLayout(QLayout* layout);

        static QString createAnimationTempIODir(const QString& tempDirName);
        
        static bool deleteAnimationTempIODir(const QString& path);

        static bool copyAndRenameFile(const QString& srcFilePath, const QString& dstFilePathWithName);

        //判断一个点是否在某一个平面某一侧
        static bool isPointKeptByPlane(
            double px, double py, double pz, //点坐标
            double ox, double oy, double oz, //平面原点
            double nx, double ny, double nz, //平面法向
            double eps = 1e-9);  //对比精度
    };

}

#endif // QVTK_COMMONFUNCTION_H
